<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="商品标题" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入商品标题"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="分类" prop="categoryId">
            <categorySelect v-model="queryParams.categoryId" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="请选择状态"
              clearable
            >
              <el-option label="下架" :value="0" />
              <el-option label="正常" :value="1" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button plain icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="productList">
      <el-table-column label="商品标题" align="center" prop="title" />
      <el-table-column label="封面图" align="center">
        <template #default="scope">
          <el-image
            :src="scope.row.coverImage"
            preview-teleported
            :preview-src-list="[scope.row.coverImage]"
            fit="cover"
            :limit="1"
            style="width: 50px; height: 50px"
          />
        </template>
      </el-table-column>
      <el-table-column label="销售价" align="center" prop="salePrice" />
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="销量" align="center" prop="sale" />
      <el-table-column label="状态" align="center">
        <template #default="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
            {{ scope.row.status === 1 ? "正常" : "下架" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="primary"
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <AddEdit
      v-if="addEditVisible"
      v-model="addEditVisible"
      :title="title"
      :row="curRow"
      @submit="getList"
    />
  </div>
</template>

<script setup name="MallProducts">
import { ref } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
import categorySelect from "@/components/categorySelect";
import { getMallProductPage, removeMallProduct } from "@/api/operateManage";
import AddEdit from "./addEdit.vue";

const { proxy } = getCurrentInstance();
const productList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const addEditVisible = ref(false);
const curRow = ref({});
const title = ref("");

const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  title: "",
  categoryId: "",
  status: "",
});

/** 查询商品列表 */
function getList() {
  loading.value = true;
  getMallProductPage(queryParams.value).then(({ data }) => {
    productList.value = data.records;
    total.value = data.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  title.value = "新增商品";
  addEditVisible.value = true;
  curRow.value = {};
}

/** 编辑按钮操作 */
function handleEdit(row) {
  title.value = "编辑商品";
  curRow.value = row;
  addEditVisible.value = true;
}

/** 删除按钮操作 */
function handleDelete(row) {
  ElMessageBox.confirm("是否确认删除该商品？", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    removeMallProduct(row.id).then(({ data }) => {
      if (data) {
        getList();
        ElMessage.success("删除成功");
      } else {
        ElMessage.error("删除失败");
      }
    });
  });
}

getList();
</script>
